<template>
  <div>
    <img :src="backgroundImg" alt="" style="position: absolute;height: 100vh;width: 100vw;">
    <div style="display: flex;align-items: center;justify-content: center;height: 100vh;width: 100vw;">
      <div class="login-wrap">

        <el-form label-position="left" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px"
          class="demo-ruleForm login-container">

          <el-col :span="8">
            <img :src="logo" alt="" style="width: 100%;">

          </el-col>
          <el-col :span="8">
            <div style="
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: center;
    font-weight: bold;
    font-size: 70px;">
              <div>问卷E</div>
              <div style="font-size: 50px;">SurveyE</div>
            </div>
          </el-col>
          <el-col :span="8">
            <h3 class="title">欢迎登录</h3>
            <div class="desc">账号密码登录</div>
            <el-form-item prop="username">
              <el-input type="text" v-model="ruleForm.username" auto-complete="off" placeholder="请输入您的账号">
                <i slot="prefix" class="el-input__icon el-icon-user"></i></el-input>
             
            </el-form-item>
            <el-form-item prop="password">
              <el-input type="password" v-model="ruleForm.password" auto-complete="off" placeholder="请输入密码">
                <i slot="prefix" class="el-input__icon el-icon-lock"></i></el-input>
            </el-form-item>

            <el-form-item>
              <!-- <el-checkbox class="remember" v-model="rememberpwd">记住密码</el-checkbox> -->
              <div class="forget">忘记密码</div>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" style="width:100%;background-color: rgba(0, 84, 254, 1);"
                @click="submitForm('ruleForm')" :loading="logining">登录</el-button>
            </el-form-item>




          </el-col>

          <!-- <el-row>
  <el-col :span="12">
    <el-form-item prop="code">
      <el-input type="text" v-model="ruleForm.code" auto-complete="off" placeholder="图形验证码" @keyup.enter.native="submitForm('ruleForm')"></el-input>
    </el-form-item>
  </el-col>
  <el-col :span="12" class="code-box">
    <img :src="ruleForm.codeimg" alt="" class="codeimg" @click="getcode()">
  </el-col>
</el-row> -->


        </el-form>
      </div>
    </div>

  </div>
</template>
<script type="text/ecmascript-6">
import Axios from 'axios'
import { login } from '../api/userMG'
import { setCookie, getCookie, delCookie } from '../utils/util'
import md5 from 'js-md5'
export default {
  name: 'login',
  data() {
    return {
      backgroundImg: require("@/assets/img/back.png"),
      logo: require("@/assets/img/logo.png"),
      //定义loading默认为false
      logining: false,
      // 记住密码
      rememberpwd: false,
      ruleForm: {
        //username和password默认为空
        username: '',
        password: '',
        code: '',
        randomStr: '',
        codeimg: ''
      },
      //rules前端验证
      rules: {
        username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      }
    }
  },
  // 创建完毕状态(里面是操作)
  created() {
   
    // 获取图形验证码
    this.getcode()
    // 获取存在本地的用户名密码
    this.getuserpwd()

  },
  // 里面的函数只有调用才会执行
  methods: {
    // 获取用户名密码
    getuserpwd() {
      if (getCookie('user') != '' && getCookie('pwd') != '') {
        this.ruleForm.username = getCookie('user')
        this.ruleForm.password = getCookie('pwd')
        this.rememberpwd = true
      }
    },
    //获取info列表
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.logining = true
          // 测试通道，不为空直接登录
          setTimeout(async () => {
            // 发起一个post请求
            try {
              let res = await Axios({
              method: 'post',
              url: this.$baseURL+'/login',
              data: this.ruleForm
            })
            localStorage.setItem('userId',res.data.userId)
            console.log(res.data)
            localStorage.setItem('token',res.data.access_token)
            console.log(res.data.userId)
            this.logining = false
            this.$store.commit('login', 'true')
            this.$router.push({ path: '/SurveyWrite/index' })
            } catch (error) {
              this.$message({
          message: '用户名或密码输入错误！',
          type: 'warning'
        });
          this.logining = false
            }
          }, 1000)
          Axios.post()

        } else {

          this.getcode()
          this.$message.error('请输入用户名密码！')
          this.logining = false
          return false
        }
      })
    },
  }
}
</script>

<style scoped>
.login-wrap {
  position: relative;
  z-index: 2;
  display: flex;
  box-sizing: border-box;
  width: 100%;
  height: 90%;
  /* padding-top: 180px; */

  /* background-color: #112346; */
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
}

.login-container {

  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 60px;
  margin: 0px auto;
  width: 75vw;
  /* height: 705px; */
  padding: 30px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}

.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
  font-style: italic;
  font-size: 50px;
}

.desc {
  margin: 0px auto 40px auto;
  text-align: center;
  color: rgba(0, 84, 254, 1);
  font-size: 18px;
  width: 6em;
  padding-bottom: 26px;
  border-bottom: 3px solid rgba(0, 84, 254, 1);
  font-weight: bold;
}

.remember {
  margin: 0px 0px 35px 0px;
}

.code-box {
  text-align: right;
}

.forget {
  color: rgba(102, 102, 102, 1);
  font-size: 14px;
  font-weight: bold;
  text-align: right;

  text-decoration: underline;
  /* border-bottom: 1px solid rgba(102, 102, 102, 1); */
}

.codeimg {
  height: 40px;
}
</style>